package grammar;


import java.util.List;


public class Converter { 
	
	public static String infixToPrefix(String string){
		
		String prefix = "";
		
		String[] or = string.split(" OR ");
		String[][] and = new String[or.length][];
		
		for(int i=0; i<or.length; i++){
			and[i] = or[i].split(" AND ");
			try{
				or[i] = "AND+" + and[i][0] + "+" + and[i][1];
			} catch(Exception e){
				or[i] = and[i][0];
			}
		}
		
		for(int i=0; i<or.length-1; i++){
			prefix += "OR+";
		}
		
		for(int i=0; i<or.length; i++){
			if (i<or.length-1) 
				prefix += or[i] + "+";
			else
				prefix += or[i];
		}
		
		return prefix;
		
	}
	
	public static String prefixToInfix(String string){
		List<String> list;
		List<String> li = PrerequisiteTokenizer.tokenize(string);
		String temp = "";
		
		Node root = new Node(li.get(0));
		
		for (int i=1; i<li.size(); i++){
			Node.insertNode(root, li.get(i));
		}
		
		list = Node.getPrerequisites(root);
		
		try{
		temp += list.get(0);
		}catch (Exception e){}
		
		for (int i=1; i<list.size(); i++){
			temp += " OR " + list.get(i);
		}
		
		return temp;
	}

	
}
